package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CountRateActivity extends AppCompatActivity implements TextWatcher{
    private static final String TAG = "CountRateActivity";
    EditText rmb_text;
    TextView rate_result;
    String rate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_rate);
        rmb_text=findViewById(R.id.RMB);
        rmb_text.addTextChangedListener(this);

        /*
        rmb_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //获取用户输入数据

            }
        });

         */

        TextView country_text=findViewById(R.id.country);
        rate_result=findViewById(R.id.rateResult);
        Intent intent =getIntent();
        String country=intent.getStringExtra("count_country");
        rate=intent.getStringExtra("count_rate");
        Log.i(TAG,"CountRateActivity:country="+country);
        Log.i(TAG,"CountRateActivity:rate="+rate);
        //放入控件中
        country_text.setText(country);

    }
    @Override
    public void afterTextChanged(Editable str) {
        if (str != null && str.length() > 0) {
            //计算结果
            float rmb1 = Float.valueOf(str.toString());
            float res = rmb1 * (100f/Float.valueOf(rate));
            //显示输出
            rate_result.setText(""+res);
        } else {
            //提示用户输入
            Toast.makeText(this, "请输入RMB金额", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {


    }


}